/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package org.apache.directory.mavibot.btree;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * A LRU cache use by a BTree 
 *
 * @param <K> The Key type
 * @param <V> The Value type
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class CacheLRU  extends LinkedHashMap<Object, Object>
{
	private static final long serialVersionUID = 1L;
	
	/** correspond to the max size of the cache */
	private int maxSize;
	
	/** correspond to the statistics of the cache */
	private CacheStatistics cacheStat;
	
	private boolean isClose=false;

	/**
	 * create a new cache, with initialization of the max size
	 * @param size
	 */
	public CacheLRU( int size )
	{
		super( size, 0.75F, true );
		this.maxSize=size;
		cacheStat=new CacheStatistics( size );
	}
	
	public synchronized Object getValue( Object key ) throws IllegalStateException
	{
		checkIsAlive();
		Object obj=super.get( key );
		boolean isPresent=true;
		if( obj==null )
		{
			isPresent=false;
		}
		//update the statistics value of the cache
		cacheStat.UpdateStatistics( isPresent, false );
		return obj;
	}
	
	public synchronized Object put( Object key, Object value ) throws IllegalStateException
	{
		checkIsAlive();
		Object val=null;
		val=super.put( key, value );
		//update the statistics value of the cache
		cacheStat.UpdateStatistics( true, true );
		return val;
	}
	
	/**
	 * access to the CacheStatistics
	 * @return
	 */
	public synchronized CacheStatistics getCacheStat()
	{
		return cacheStat;
	}
	
	public synchronized boolean isClose()
	{
		return isClose;
	}

	public synchronized void setClose( boolean isClose ) 
	{
		this.isClose = isClose;
	}
	
	/**
	 * call the method remove for each key of the CacheLRU
	 */
	public synchronized void removeAll() throws IllegalStateException
	{
		checkIsAlive();
		Set<Object> listSet=new HashSet<Object>();
		for( Object k : keySet() )
		{
			listSet.add( k );
		}
		for( Object k : listSet )
		{
			remove( k );
		}
	}
	
	private void checkIsAlive() throws IllegalStateException {
        if ( isClose ) {
            throw new IllegalStateException("The Cache is not alive.");
        }
    }

	@Override
	public boolean removeEldestEntry( Map.Entry<Object, Object> eldest )
	{
		return size() > this.maxSize;
	}
}